//
//  CCVSGraphData+Utilities.m
//  ClinicalCollections
//
//  Created by Pasirstein, David B on 5/2/13.
//  Copyright (c) 2013 GOPI. All rights reserved.
//

#import "CCVSGraphData+Utilities.h"


@implementation CCVSGraphData (Utilities)


+(NSArray *)removeTotalFromArray:(NSArray *) theDataArray
{
    NSMutableArray *dataArray = [theDataArray mutableCopy];
    
    int totalPosition = -1;
    for (int i=0; i< [dataArray count]; i++)
    {
        NSDictionary *element = [dataArray objectAtIndex:i];
        if ([[element allKeys] count] && [[element allKeys][0] isEqualToString:@"Total"]) //Do not consider Total as a value to be plotted
            totalPosition = i;
    }
    if (totalPosition > -1)
        [dataArray removeObjectAtIndex:totalPosition];
    
    return [dataArray copy];
}



#define CHECK_DATA_EXISTS(x) [[x valueForKey:DATA] count] > 0

+ (NSMutableArray*) loadAllClinicalTrialGovGraphData:(ClinicalTrialGov *)clinicalTrialGov
{
    
    NSMutableArray * overallStudy;
    if (clinicalTrialGov != nil) {
        
        overallStudy = [[NSMutableArray alloc] initWithCapacity:MAX_CAPACITY];
        
        NSMutableDictionary *dictionary = [[NSMutableDictionary alloc] initWithObjectsAndKeys:OVERALL_STUDY, DESCRIPTION,
                                           [[CommonAPI class] loadData:clinicalTrialGov
                                                             groupType:BASELINE
                                                          measureTitle:NUMBER_OF_PARTICIPANTS
                                                           measureUnit:nil
                                                         categoryTitle:nil
                                                      categorySubtitle:nil],DATA, BAR_CHART, CHART_TYPE, nil];
        if (CHECK_DATA_EXISTS(dictionary))
            [overallStudy addObject:dictionary];
        NSArray * distinctCategorySubtitle = [[CommonAPI class] getCategorySubtitle:(ClinicalTrialGov *)clinicalTrialGov
                                                                          groupType:@"participant_flow"
                                                                       measureTitle:nil];
        
        for (NSString *categorySubtitle in distinctCategorySubtitle) {
            dictionary = [[NSMutableDictionary alloc] initWithObjectsAndKeys:categorySubtitle, @"description",
                          [[CommonAPI class] loadData:clinicalTrialGov
                                            groupType:@"participant_flow"
                                         measureTitle:@"Overall Study"
                                          measureUnit:nil
                                        categoryTitle:nil
                                     categorySubtitle:categorySubtitle],@"data" , PIE_CHART, CHART_TYPE, nil];
            if (CHECK_DATA_EXISTS(dictionary))
                [overallStudy addObject:dictionary];
        }
        distinctCategorySubtitle = [[CommonAPI class] getCategorySubtitle:(ClinicalTrialGov *)clinicalTrialGov
                                                                groupType:@"baseline"
                                                             measureTitle:@"Age"];
        
        for (NSString *categorySubtitle in distinctCategorySubtitle) {
            dictionary = [[NSMutableDictionary alloc] initWithObjectsAndKeys:categorySubtitle, @"description",
                          [[CommonAPI class] loadData:clinicalTrialGov
                                            groupType:@"baseline"
                                         measureTitle:@"Age"
                                          measureUnit:@"participants"
                                        categoryTitle:nil
                                     categorySubtitle:categorySubtitle],@"data" , PIE_CHART, CHART_TYPE, nil];
            if (CHECK_DATA_EXISTS(dictionary))
                [overallStudy addObject:dictionary];
        }
        distinctCategorySubtitle = [[CommonAPI class] getCategorySubtitle:(ClinicalTrialGov *)clinicalTrialGov
                                                                groupType:@"baseline"
                                                             measureTitle:@"Gender"];
        for (NSString *categorySubtitle in distinctCategorySubtitle) {
            dictionary = [[NSMutableDictionary alloc] initWithObjectsAndKeys:categorySubtitle, @"description",
                          [[CommonAPI class] loadData:clinicalTrialGov
                                            groupType:@"baseline"
                                         measureTitle:@"Gender"
                                          measureUnit:@"participants"
                                        categoryTitle:nil
                                     categorySubtitle:categorySubtitle],@"data" , PIE_CHART, CHART_TYPE, nil];
            if (CHECK_DATA_EXISTS(dictionary))
                [overallStudy addObject:dictionary];
        }
        distinctCategorySubtitle = [[CommonAPI class] getCategorySubtitle:(ClinicalTrialGov *)clinicalTrialGov
                                                                groupType:@"baseline"
                                                             measureTitle:@"Region of Enrollment"];
        for (NSString *categorySubtitle in distinctCategorySubtitle) {
            
            dictionary = [[NSMutableDictionary alloc] initWithObjectsAndKeys:categorySubtitle, @"description",
                          [[CommonAPI class] loadData:clinicalTrialGov
                                            groupType:@"baseline"
                                         measureTitle:@"Region of Enrollment"
                                          measureUnit:@"participants"
                                        categoryTitle:nil
                                     categorySubtitle:categorySubtitle],@"data" , PIE_CHART, CHART_TYPE, nil];
            if (CHECK_DATA_EXISTS(dictionary))
                [overallStudy addObject:dictionary];
            
            
        }
        dictionary = [[NSMutableDictionary alloc] initWithObjectsAndKeys:@"WithDraw", @"description",
                      [[CommonAPI class] loadData:clinicalTrialGov
                                        groupType:@"participant_flow"
                                     measureTitle:@"Overall Study"
                                      measureUnit:nil
                                    categoryTitle:@"WithDrawCounts"
                                 categorySubtitle:nil],@"data" ,PIE_CHART, CHART_TYPE, nil];
        if (CHECK_DATA_EXISTS(dictionary))
            [overallStudy addObject:dictionary];
        
        
        NSArray *outcomes = [CommonAPI getOutcomes:clinicalTrialGov];
        
        for (GroupTypes *outcome in outcomes) {
            NSArray *measures = [outcome.measure allObjects];
            for (Measure *measure in measures) {
                
                NSArray *categories = [measure.categories allObjects];
                
                if([measure.title isEqual:@"Number of Participants"]) continue;
                
                for (Categories *category in categories) {
                    int r = rand() % 2;
                    
                    dictionary = [[NSMutableDictionary alloc] initWithObjectsAndKeys:[NSString stringWithFormat:@"%@,%@,%@", measure.param, measure.unit, measure.dispersion], @"description",
                                  [[CommonAPI class] loadData:clinicalTrialGov
                                                    groupType:outcome.type
                                                 measureTitle:measure.title
                                                  measureUnit:nil
                                                categoryTitle:nil
                                             categorySubtitle:nil],@"data" ,(r%2 == 0) ? BAR_CHART:PIE_CHART, CHART_TYPE, nil];
                    if (CHECK_DATA_EXISTS(dictionary))
                        [overallStudy addObject:dictionary];
                    
                }
                
            }
        }
    }
    return overallStudy;
}



@end
